import React from 'react';
import { connect } from 'react-redux';
import { hashHistory } from 'react-router';

import Spin from 'antd/lib/Spin';

import { getUserInfo } from '../actions/sign';
import { Link } from 'react-router';

import getCookieName from '../utils/cookieHelper';

let styles = {
    parentPanel: {
        paddingTop: "40%",
        textAlign: "center"
    },
    loadingText: {
        paddingTop: "1rem",
        fontSize: "0.9rem"
    }
}

class Main extends React.Component {
    componentDidMount() {
        this.props.getUserInfo(this.props.params.openid);
    }
    componentWillReceiveProps(nextProps) {
        let path = '';
        if (this.props.params!== undefined && this.props.params.url !=undefined && this.props.params.url.length > 0) {
            path = this.props.params.url;
        }
        getCookieName("token");
        if (path.length <= 0 &&nextProps.userInfo !== undefined && (nextProps.userInfo.user.UserStatus === 'Approved'||(nextProps.userInfo.user.UserStatus === 'Pending'&&nextProps.userInfo.user.Roles.indexOf('Reviewer')>=0))) {
            hashHistory.push('home');
        } else if (path == "legislation") {
            hashHistory.push(path)
        } else if (path.length > 0 &&nextProps.userInfo !== undefined && (nextProps.userInfo.user.UserStatus === 'Approved'||(nextProps.userInfo.user.UserStatus === 'Pending'&&nextProps.userInfo.user.Roles.indexOf('Reviewer')>=0))) {
            if(path == 'reviewer'){
                hashHistory.push("home");
            } else if (this.props.params.param1===undefined) {
                hashHistory.push(path)
            } else {
                hashHistory.push(path + '/' + this.props.params.param1);
            }
        } else {
            if (path.length > 0 && path == 'article') {
                sessionStorage.targetUrl = path+'/'+ this.props.params.param1;
            } else if (path.length > 0 && path == 'reviewer') {
                hashHistory.push('reviewer/' + nextProps.params.openid);   
                return; 
            }
            hashHistory.push('register/' + nextProps.params.openid);
        } 
    }
    render() {
        return (
            <div style={styles.parentPanel}>
                <Spin size="large"/>
                <p style={styles.loadingText}>努力加载中......</p>
            </div>
        );
    }
}

function mapStateToProps(state) {
    return {
        userInfo: state.sign.userInfo,
    }
}

export default connect(mapStateToProps, { getUserInfo })(Main);